package com.ss.whisper2.service.impl;

import com.ss.whisper2.dao.UserDao;
import com.ss.whisper2.exception.GlobalException;
import com.ss.whisper2.pojo.State;
import com.ss.whisper2.pojo.entity.user.RequestUser;
import com.ss.whisper2.pojo.entity.user.User;
import com.ss.whisper2.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	UserDao userDao;
	@Override
	public User validate(String reqUsername, String reqPassword) {
		User dbUser=userDao.getUser(reqUsername);
		if (dbUser == null) {
			throw new GlobalException(State.EMPTY_USER);
		}
		if (!dbUser.getPassword().equals(reqPassword)) {
			throw new GlobalException(State.ERROR_PASSWORD);
		}
		return dbUser;
	}

	@Override
	public List<User> getUserList() {
		return userDao.getUserList();
	}

	@Override
	public void addUser(RequestUser reqUser) {
		userDao.insertUser(reqUser);
	}

	@Override
	public void deleteUser(String username) {
		userDao.deleteUser(username);
	}

	@Override
	public void setUser(RequestUser reqUser) {
		userDao.setUser(reqUser);
	}

	@Override
	public void setUserPassword(RequestUser reqUser) {
		userDao.setUserPassword(reqUser);
	}

	@Override
	public void recordLoginUser(String username) {
		userDao.recordLoginUser(username);
	}
}
